package org.haier.aws.lambda;

import com.amazonaws.services.iot.AWSIot;
import com.amazonaws.services.iot.model.RegisterCertificateRequest;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import org.haier.aws.dto.CustomRegisterDto;
import org.haier.aws.utils.AWSIotUtils;
import org.haier.aws.utils.JsonUtils;

/**
 * 自定义注册流程
 * @author: SongKun
 * @date: 2021/5/13
 */
public class CustomRegister implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

    @Override
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
        LambdaLogger logger = context.getLogger();
        String body = input.getBody();
        CustomRegisterDto dto = JsonUtils.parse(body, CustomRegisterDto.class, logger);
        logger.log("请求体为：" + body);
        AWSIot awsIot = AWSIotUtils.getAwsIot();
        // 手动注册证书
        awsIot.registerCertificate(new RegisterCertificateRequest().withCaCertificatePem("").withCertificatePem(""));
        // 注册证书
        // CreateCertificateFromCsrResult csr = awsIot.createCertificateFromCsr(new CreateCertificateFromCsrRequest()
        //        .withCertificateSigningRequest(dto.getCertificateSigningRequest()));
        // String certificateArn = csr.getCertificateArn();
        // String certificateId = csr.getCertificateId();
        // logger.log("certificateArn:" + certificateArn );
        // logger.log("certificateId:" +certificateId);

        return null;
    }
}
